package demo2;

import demo1.MyArraylist;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.ListIterator;
public class Test3 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);

        System.out.println("for循环：");
        for(int i = 0; i < list.size();i++){
            System.out.print(list.get(i)+" ");
        }
        System.out.println();

        System.out.println("for each循环：");
        for(int x:list){
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("迭代器遍历1：");
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();

        System.out.println("迭代器循环2：");
        ListIterator<Integer> lit = list.listIterator();
        while(lit.hasNext()){
            System.out.print(lit.next()+" ");
        }
        System.out.println();

        System.out.println("迭代器循环3：");
        ListIterator<Integer> litPrev = list.listIterator(list.size());
        while(litPrev.hasPrevious()){
            System.out.print(litPrev.previous()+" ");
        }
        System.out.println();
    }
    public static void main3(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0;i<6;i++){
            list.add(i);
        }
        System.out.println(list);

        List<Integer> listTmp = list.subList(1,3);
        System.out.println(listTmp);
        System.out.println("=============");
        listTmp.set(1,888);
        System.out.println("listTmp "+listTmp);
        System.out.println("list "+list);

        list.remove(new Integer(1));
        System.out.println(list);
        list.remove(2);
        System.out.println(list);

        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(11);
        list2.add(22);
        list2.add(33);
        list.addAll(list2);
        System.out.println(list);
    }
    public static void main2(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for(int i = 0;i<6;i++){
            list.add(i);
        }
        System.out.println(list);
        list.add(1,999);
        System.out.println(list);
    }
    public static void main1(String[] args) {
        MyArraylist myArraylist = new MyArraylist();
        myArraylist.add(1);
        myArraylist.add(2);
        myArraylist.add(3);

        myArraylist.display();
        System.out.println(myArraylist.contains(3));
        System.out.println(myArraylist.get(1));
        //myArraylist.set(99,3);
        myArraylist.set(2,8);
        myArraylist.display();
    }
}
